#include "mat_gemcollect.h"
#include "mat_sounds.h"
#include "mat_game.h"

#include "ui/ui_sprite.h"
#include "num/rand.h"
#include "math/math_core.h"
#include "ui/ui_manager.h"

#include "util/common_macros.h"



MATGemCollect::MATGemCollect() 
{
	ui::UIWidget* pChestPos = ui::UIManager::Instance().GetWidget("pos_chest");
	if(pChestPos)
		m_PosTarget = pChestPos->GetPosition();
}

MATGemCollect::~MATGemCollect()
{
	for(int i = m_pCollectedGems.size() -1; i >= 0; i--)
	{
		UTIL_SAFE_DELETE(m_pCollectedGems[i]);
	}
	m_pCollectedGems.resize(0);

	
	
	  
}

MATGemCollect&		MATGemCollect::Instance()
{
	static MATGemCollect inst;
	return inst;
}


void	MATGemCollect::Tick(float dt)
{

	const math::Vec2 Target = m_PosTarget;
	MATGemCollect::CollectedGem* pGem;
	for(int i = 0; i < (int)m_pCollectedGems.size(); i++)
	{
		if(!m_pCollectedGems[i]->m_IsFinish)
		{
			pGem = m_pCollectedGems[i];
			pGem->m_Delay -= dt;
			if(pGem->m_Delay > 0)
				continue;

			pGem->m_Delay = 0;
			pGem->m_Rotation += ( pGem->m_RotationSpeed * dt * 3.0f);
			math::Vec2 Dir = Target - pGem->m_Pos;
					
			Dir.Normalize();
			const float RADIUS = Dir.Magnitude();			
			
			
			pGem->m_ArcCounter += dt;
			pGem->m_Accel += dt;
			
			pGem->m_Pos += (Dir * dt * pGem->m_Accel);

			if(pGem->m_ArcCounter > 0.5f)
				pGem->m_ArcCounter = 0;

			if(pGem->m_ArcDir == 1)
				pGem->m_Pos.x += (cosf( pGem->m_ArcCounter * MATH_2PI )  * RADIUS *  dt ); 
			else
				pGem->m_Pos.x -= (cosf( pGem->m_ArcCounter * MATH_2PI )  * RADIUS *  dt ); 


			Dir = Target - pGem->m_Pos;
			if(pGem->m_Scale > 0.7f)
			{
				pGem->m_Scale -= dt;
				if(pGem->m_Scale < 0.7f)
					pGem->m_Scale = 0.7f;
			}

            if ( Dir.Magnitude() < 0.025f)	// reach
			{
				pGem->m_IsFinish = true;	
				MATSounds::Instance().PlaySoundSFX(MATSounds::SFX_GEM_COLEECTED);
			}
		}
	}

}

void	MATGemCollect::Add(const math::Vec2& Pos, ui::UISprite* pSprite, float Delay)
{
	MATGemCollect::CollectedGem* pCGem = NULL;
	for(int i = 0; i < (int)m_pCollectedGems.size(); i++)
	{
		if(m_pCollectedGems[i]->m_IsFinish)
		{
			pCGem = m_pCollectedGems[i];
			i = (int)m_pCollectedGems.size();
		}
	}

	if(!pCGem)
	{
		pCGem = new MATGemCollect::CollectedGem();
		m_pCollectedGems.push_back(pCGem);
	}


	pCGem->m_IsFinish = false;
	pCGem->m_Pos = Pos;
	pCGem->m_pSprite = pSprite;
	pCGem->m_Delay = Delay;
	pCGem->m_RotationSpeed = num::RAND32::Instance().RandInt(-360,360);
	pCGem->m_Rotation= 0;
	pCGem->m_ArcCounter = 0;
	pCGem->m_Accel = 1.0f;
	pCGem->m_ArcDir = num::RAND32::Instance().RandInt(1,2);
	pCGem->m_Scale = 1.0f;
	pCGem->m_Size = MATGame::GetGemSize();

	
}


void	MATGemCollect::Render()
{
	for(int i = 0; i < (int)m_pCollectedGems.size(); i++)
	{
		if(!m_pCollectedGems[i]->m_IsFinish)
		{
			if(m_pCollectedGems[i]->m_Delay > 0)
				continue;

			m_pCollectedGems[i]->m_pSprite->m_Pos = m_pCollectedGems[i]->m_Pos;
			m_pCollectedGems[i]->m_pSprite->m_Rotation += m_pCollectedGems[i]->m_Rotation;
			m_pCollectedGems[i]->m_pSprite->RenderRotated();
			m_pCollectedGems[i]->m_pSprite->m_Rotation = 0;
			m_pCollectedGems[i]->m_pSprite->m_Size = m_pCollectedGems[i]->m_Size *m_pCollectedGems[i]->m_Scale;

		}
	}
}


